package handler;

import beans.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
// 拦截器，对每次请求进行身份识别
public class LoginInterceptor implements HandlerInterceptor{

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        if (user!=null){
            return true;
        }
        // 身份验证失败则，返回登录页面进行登录；
        request.setAttribute("cc_error","您尚未登录，请先登录");
        request.getRequestDispatcher("/index.jsp").forward(request,response);
        return false;
    }
}
